﻿using Hidistro.Core;
using Hidistro.Entities.Commodities;
using Hidistro.SaleSystem.Catalog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Hidistro.UI.SaleSystem.Tags
{
    public class wnxd_ListRank : WebControl
    {

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.html());
        }
        private string html()
        {
            StringBuilder sb = new StringBuilder();
            int i = 0;
            int.TryParse(this.Page.Request.QueryString["rank"], out i);

            if (string.IsNullOrEmpty(this.Page.Request.QueryString["Staticize"]))
            {
                sb.AppendFormat("<ul>");
                sb.AppendFormat("<li><a{0} href=\"{1}\">综合排序</a></li>", i == 0 ? " class=\"wewes\"" : "", this.CreateUrl("rank", "0"));
                sb.AppendFormat("<li><a href=\"{0}\"{1}>销量优先<span></span></a></li>", this.CreateUrl("rank", "1"), i == 1 ? " class=\"wewes\"" : "");
                sb.AppendFormat("<li><a href=\"{0}\"{1}>人气优先<span></span></a></li>", this.CreateUrl("rank", "3"), i == 3 ? " class=\"wewes\"" : "");
                sb.AppendFormat("<li><a href=\"{0}\"{1}>价格最低</a></li>", this.CreateUrl("rank", "6"), i == 6 ? " class=\"wewes\"" : "");
                sb.AppendFormat("<li><a href=\"{0}\"{1}>价格最高</a></li>", this.CreateUrl("rank", "5"), i == 5 ? " class=\"wewes\"" : "");
                sb.AppendFormat("<li><a href=\"{0}\"{1}>最新上架</a></li>", this.CreateUrl("rank", "7"), i == 7 ? " class=\"wewes\"" : "");
                sb.AppendFormat("</ul>");
            }
            else
            {
                sb.AppendFormat("<ul>");
                sb.AppendFormat("<li><a{0} href=\"{1}\">综合排序</a></li>", i == 0 ? " class=\"wewes\"" : "", this.CreateStaticizeUrl(0));
                sb.AppendFormat("<li><a href=\"{0}\"{1}>销量优先<span></span></a></li>", this.CreateStaticizeUrl(1), i == 1 ? " class=\"wewes\"" : "");
                sb.AppendFormat("<li><a href=\"{0}\"{1}>人气优先<span></span></a></li>", this.CreateStaticizeUrl(3), i == 3 ? " class=\"wewes\"" : "");
                sb.AppendFormat("<li><a href=\"{0}\"{1}>价格最低</a></li>", this.CreateStaticizeUrl(6), i == 6 ? " class=\"wewes\"" : "");
                sb.AppendFormat("<li><a href=\"{0}\"{1}>价格最高</a></li>", this.CreateStaticizeUrl(5), i == 5 ? " class=\"wewes\"" : "");
                sb.AppendFormat("<li><a href=\"{0}\"{1}>最新上架</a></li>", this.CreateStaticizeUrl(7), i == 7 ? " class=\"wewes\"" : "");
                sb.AppendFormat("</ul>");
            }
            return sb.ToString();
        }
        private string CreateUrl(string paraName, string paraValue)
        {
            string rawUrl = this.Context.Request.RawUrl;
            if (rawUrl.IndexOf("?") >= 0)
            {
                string oldValue = rawUrl.Substring(rawUrl.IndexOf("?") + 1);
                string[] strArray = oldValue.Split(new char[] { Convert.ToChar("&") });
                rawUrl = rawUrl.Replace(oldValue, "");
                foreach (string str3 in strArray)
                {
                    if (!str3.ToLower().StartsWith(paraName + "=") && !str3.ToLower().StartsWith("pn="))
                    {
                        rawUrl = rawUrl + str3 + "&";
                    }
                }
                return (rawUrl + paraName + "=" + Globals.UrlEncode(paraValue));
            }
            string str4 = rawUrl;
            return (str4 + "?" + paraName + "=" + Globals.UrlEncode(paraValue));
        }

        private string CreateStaticizeUrl(int pos)
        {
            if (this.Page.Request.Path == "/SubCategory.aspx")
            {
                int feature;
                int brand;
                int filter;
                int rank;

                int.TryParse(this.Page.Request.QueryString["feature"], out feature);
                int.TryParse(this.Page.Request.QueryString["brand"], out brand);
                int.TryParse(this.Page.Request.QueryString["filter"], out filter);
                int.TryParse(this.Page.Request.QueryString["rank"], out rank);

                int categoryId;

                int.TryParse(this.Page.Request.QueryString["categoryId"], out categoryId);


                CategoryInfo cateif = CategoryBrowser.GetCategory(categoryId);

                string rawUrl = this.Context.Request.RawUrl;
                if (rawUrl.ToLower().Contains("/brand"))
                {
                    if (cateif == null)
                        return Globals.GetSiteUrls().StaticizeBrand(0, filter, brand, pos, 0);
                    else
                        return Globals.GetSiteUrls().StaticizeBrand(cateif.CategoryId, filter, brand, pos, 0);
                }
                else
                {
                    if (cateif == null)
                        return Globals.GetSiteUrls().StaticizeSubCategory(0, "", 0, "", feature, brand, filter, pos, 0);
                    else
                        return Globals.GetSiteUrls().StaticizeSubCategory(cateif.CategoryId, cateif.RewriteName, (int)cateif.ParentCategoryId, cateif.ParentRewriteName, feature, brand, filter, pos, 0);
                }
            }
            else if(this.Page.Request.Path == "/BrandDetails.aspx")
            {
                int cid;
                int brand;
                int filter;
                int rank;

                string model = "B{0}C{1}F{2}R{3}.html";

                string url;
                int.TryParse(this.Page.Request.QueryString["cid"], out cid);
                int.TryParse(this.Page.Request.QueryString["brandId"], out brand);
                int.TryParse(this.Page.Request.QueryString["filter"], out filter);
                int.TryParse(this.Page.Request.QueryString["rank"], out rank);

                url = string.Format(model, brand, cid, filter, pos);


                return url;

            }
            return "";
        }

    }
}
